<?php

namespace backend\modules\business\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\business\BusinessInspection;

/**
 * BusinessInspectionSearch represents the model behind the search form about `common\models\business\BusinessInspection`.
 */
class BusinessInspectionSearch extends BusinessInspection
{

    public $pigno;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'business_person','pigid','business_personid','status','is_del'], 'integer'],
            [['healthy','weight', 'height', 'length', 'width', 'pigpen','pigno','pigpen_desc', 'pigpen_photo', 'feed', 'feed_desc', 'feed_photo','createtime'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }


    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = BusinessInspection::find();

        // add conditions that should always apply here

        $query = $query->joinWith(['pig']);


//        $query = $query->leftJoin(BusinessPig::tableName(),'id=pigid');

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'createtime' => SORT_DESC,
                ],
            ],
        ]);

        $dataProvider->setSort([
            'attributes' => [
                'business_personid',
                'createtime',
                'height',
                'status',
                'pigno' => [
                    'asc' => ['no' => SORT_ASC],
                    'desc' => ['no' => SORT_DESC],
                ],
            ]
        ]);




        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }


        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'business_personid' => $this->business_personid,
            'pigid' => $this->pigid,
            'healthy' => $this->healthy,
            BusinessInspection::tableName().'.status' => $this->status,
            'is_del' => $this->is_del
        ]);

        $query->andFilterWhere(['like', 'weight', $this->weight])
//            ->andFilterWhere(['like', 'person', $this->weight])
            ->andFilterWhere(['like', 'height', $this->height])
            ->andFilterWhere(['like', 'length', $this->length])
            ->andFilterWhere(['like', 'width', $this->width])
            ->andFilterWhere(['like', 'pigpen', $this->pigpen])
            ->andFilterWhere(['like', 'feed', $this->feed])
            ->andFilterWhere(['like', 'no', $this->pigno])
            ->andFilterWhere(['FROM_UNIXTIME(createtime, \'%Y-%m-%d\')'=>$this->createtime]);

        return $dataProvider;
    }
}
